#!/bin/bash
BASE_COLLECTION_PATH="/must-gather"
WINDOWS_NODE_LOGS=$BASE_COLLECTION_PATH/host_service_logs/windows

# Services logging to WinEvent log
SERVICES=(docker)

# Logfile list
LOGS=(kube-proxy/kube-proxy.exe.INFO kube-proxy/kube-proxy.exe.ERROR kube-proxy/kube-proxy.exe.WARNING)
LOGS+=(hybrid-overlay/hybrid-overlay.log kubelet/kubelet.log)

# if the cluster has no Windows nodes skip this script
WIN_NODES=$(/usr/bin/oc get no -l kubernetes.io/os=windows)
if [ -z "$WIN_NODES" ]; then
    exit 0
fi

PIDS=()
LOG_WINEVENT_DIR=${WINDOWS_NODE_LOGS}/log_winevent/
mkdir -p ${LOG_WINEVENT_DIR}
echo INFO: Collecting logs for all Windows nodes
for service in ${SERVICES[@]}; do
    echo "INFO: Collecting WinEvent application logs for provider $service"
    /usr/bin/oc adm node-logs -l kubernetes.io/os=windows -u $service > ${LOG_WINEVENT_DIR}/${service}_winevent.log &
    PIDS+=($!)
done
for log in ${LOGS[@]}; do
    LOG_FILE_DIR=${WINDOWS_NODE_LOGS}/log_files/$(dirname $log)
    mkdir -p ${LOG_FILE_DIR}
    /usr/bin/oc adm node-logs -l kubernetes.io/os=windows --path=$log > ${LOG_FILE_DIR}/$(basename $log) &
    PIDS+=($!)
done
wait ${PIDS[@]}

# force disk flush to ensure that all data gathered is accessible in the copy container
sync
